package cn.com.css.taxInspection.workflow.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.css.taxInspection.workflow.mapper.WorkflowLogSurveyMapper;
import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.common.result.DataResult;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.com.css.taxInspection.workflow.entity.WorkflowLogSurveyContentEntity;
import cn.com.css.taxInspection.workflow.entity.WorkflowLogSurveyEntity;
import cn.com.css.taxInspection.workflow.service.WorkflowLogSurveyContentService;
import cn.com.css.taxInspection.workflow.service.WorkflowLogSurveyService;

@Service("workflowLogSurveyService")
public class WorkflowLogSurveyServiceImpl extends ServiceImpl<WorkflowLogSurveyMapper, WorkflowLogSurveyEntity> implements WorkflowLogSurveyService {
	
	@Autowired
	private WorkflowLogSurveyContentService surveyContentService;
	@Autowired
	private HttpSessionService httpSessionService;

	@Override
	@DS("oracle")
	public DataResult saveSurveyAndContent(WorkflowLogSurveyContentEntity surveyContentEntity) {
		String currentUserId = httpSessionService.getCurrentUserId();
		String currentDeptId = httpSessionService.getCurrentDeptId();
		Date date = new Date();
		String surveyId = TsBqUtils.getUUID();
		String surveyTitle = surveyContentEntity.getSurveyTitle();
		String surveyContent = surveyContentEntity.getOptionsConent();
		JSONArray surveyContentJsonObject = JSON.parseArray(surveyContent);
//		 JSONObject surveyContentJsonObject = JSONArray.parseObject(surveyContent);
		int wjJsonSize = surveyContentJsonObject.size();
		List<WorkflowLogSurveyContentEntity> listSurveyContent = new ArrayList<>();
		for (int i = 0; i < wjJsonSize; i++) {
			JSONObject optionsJsonObject = JSONObject.parseObject(surveyContentJsonObject.get(i).toString());
			String optionsType = optionsJsonObject.get("optionsType").toString();
			String name = TsBqUtils.getUUID();
			String optionsTitle = optionsJsonObject.get("optionsTitle").toString();
			String surveyContentId = TsBqUtils.getUUID();
			String _name = i + "_" + optionsType + "_" + name;
			String optionsContent = optionsJsonObject.get("options").toString();
			WorkflowLogSurveyContentEntity _tsSqBsSurveyContentEntity = new WorkflowLogSurveyContentEntity();
			if (optionsType.equals(Constants.SURVEY_TYPE_OPTION_1)) {
				// 文本逻辑
				_tsSqBsSurveyContentEntity.setId(surveyContentId);// 问卷选项id
				_tsSqBsSurveyContentEntity.setSurveyId(surveyId);// 问卷id
				_tsSqBsSurveyContentEntity.setOptionsRelation(_name);// 问卷选项id
				_tsSqBsSurveyContentEntity.setOptionsTitle(optionsTitle);// 问卷选项标题
				_tsSqBsSurveyContentEntity.setOptionsConent(optionsContent);// 问卷选项内容
				_tsSqBsSurveyContentEntity.setOptionsType(optionsType);// 选项类型
				_tsSqBsSurveyContentEntity.setOptionsNo(String.valueOf(i + 1)); // 选项序号
				_tsSqBsSurveyContentEntity.setDeleted(Constants.DELETEED_NOT);
				_tsSqBsSurveyContentEntity.setCreateId(currentUserId);
				_tsSqBsSurveyContentEntity.setCreateTime(date);
				listSurveyContent.add(_tsSqBsSurveyContentEntity);
			} else if (optionsType.equals(Constants.SURVEY_TYPE_OPTION_2)) {
				// 多选逻辑
				_tsSqBsSurveyContentEntity.setId(surveyContentId);// 问卷选项id
				_tsSqBsSurveyContentEntity.setSurveyId(surveyId);// 问卷id
				_tsSqBsSurveyContentEntity.setOptionsRelation(_name);// 问卷选项id
				_tsSqBsSurveyContentEntity.setOptionsTitle(optionsTitle);// 问卷选项标题
				_tsSqBsSurveyContentEntity.setOptionsConent(optionsContent);// 问卷选项内容
				_tsSqBsSurveyContentEntity.setOptionsType(optionsType);// 选项类型
				_tsSqBsSurveyContentEntity.setOptionsNo(String.valueOf(i + 1)); // 选项序号
				_tsSqBsSurveyContentEntity.setDeleted(Constants.DELETEED_NOT);
				_tsSqBsSurveyContentEntity.setCreateId(currentUserId);
				_tsSqBsSurveyContentEntity.setCreateTime(date);
				listSurveyContent.add(_tsSqBsSurveyContentEntity);
			} else if (optionsType.equals(Constants.SURVEY_TYPE_OPTION_3)) {
				_tsSqBsSurveyContentEntity.setId(surveyContentId);// 问卷选项id
				_tsSqBsSurveyContentEntity.setSurveyId(surveyId);// 问卷id
				_tsSqBsSurveyContentEntity.setOptionsRelation(_name);// 问卷选项id
				_tsSqBsSurveyContentEntity.setOptionsTitle(optionsTitle);// 问卷选项标题
				_tsSqBsSurveyContentEntity.setOptionsConent(optionsContent);// 问卷选项内容
				_tsSqBsSurveyContentEntity.setOptionsType(optionsType);// 选项类型
				_tsSqBsSurveyContentEntity.setOptionsNo(String.valueOf(i + 1)); // 选项序号
				_tsSqBsSurveyContentEntity.setDeleted(Constants.DELETEED_NOT);
				_tsSqBsSurveyContentEntity.setCreateId(currentUserId);
				_tsSqBsSurveyContentEntity.setCreateTime(date);
				listSurveyContent.add(_tsSqBsSurveyContentEntity);
			}
		}

		// 保存数据库
		WorkflowLogSurveyEntity tsSqBsSurveyEntity = new WorkflowLogSurveyEntity();
		tsSqBsSurveyEntity.setId(surveyId);
		tsSqBsSurveyEntity.setTitle(surveyTitle);
		tsSqBsSurveyEntity.setContent(surveyContent);
		tsSqBsSurveyEntity.setTaxorgcode(currentDeptId);
		tsSqBsSurveyEntity.setState("0");
		tsSqBsSurveyEntity.setDeleted(Constants.DELETEED_NOT);
		tsSqBsSurveyEntity.setCreateId(currentUserId);
		tsSqBsSurveyEntity.setCreateTime(date);
		this.save(tsSqBsSurveyEntity);
		if (!listSurveyContent.isEmpty()) {
			for (WorkflowLogSurveyContentEntity _surveyContent : listSurveyContent) {
				surveyContentService.save(_surveyContent);
			}
		}
		return DataResult.success();
	}
}